﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Xml.Serialization;
using System.Collections.Generic;
using System;

namespace Gamev1
{
    public class Portal : Sprite
    {
        private bool increaseTransparency;
        public List<Vector2> portalPositions;
        private float angle = 0f;
        public float scale = 1.0f;
        private bool scalingUp = false;
        public float scalingRatio = 0.005f;

        public override void collisionWith(Sprite sprite)
        {
            if (sprite is Character)
            {
                Character character = (Character)sprite;
                if (character.hasKey)
                {
                    Random rand = new Random();
                    int index = rand.Next(portalPositions.Count);
                    character.Position = portalPositions[index];
                    character.hasKey = false;
                }
            }
        }

        public override void update(GameTime gameTime)
        {
            adjustAngle(gameTime);
            adjustScaling();
        }

        public override void draw(SpriteBatch spriteBatch)
        {
            Vector2 origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
            Vector2 centerPosition = new Vector2(Position.X + Texture.Width / 2, Position.Y + Texture.Height / 2);
            spriteBatch.Draw(Texture, centerPosition, null, Color, angle, origin, scale, SpriteEffects.None, 0f);
            changeTransparency();
        }

        public void adjustScaling()
        {
            if (scalingUp)
            {
                scale += scalingRatio;
            }
            else
            {
                scale -= scalingRatio;
            }
            if (scale >= 1f)
            {
                scalingUp = false;
            }
            else if (scale <= 0.25f)
            {
                scalingUp = true;
            }
        }

        public void adjustAngle(GameTime gameTime)
        {
            float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
            angle += elapsedSeconds;
        }

        private void changeTransparency()
        {
            if (Color.A == 255)
            {
                increaseTransparency = false;
            }
            if (Color.A == 0)
            {
                increaseTransparency = true;
            }
            if (increaseTransparency)
            {
                Color.A += 3;
            }
            else
            {
                Color.A -= 3;
            }
        }
    }
}
